import { PostsService, PostsRo, queryParams } from './posts.service'
import {
  Body,
  Controller,
  Delete,
  Get,
  Param,
  Post,
  Put,
  Query,
} from '@nestjs/common'
import { CreatePostDto } from './dto/create-post.dto'
import { ApiOperation } from '@nestjs/swagger/dist/decorators/api-operation.decorator'
import { ApiTags } from '@nestjs/swagger/dist/decorators/api-use-tags.decorator'
import { UpdatePostDto } from './dto/update-post.dto'
import { Public } from 'src/common/public.decorator'


@ApiTags('文章')
@Controller('post')
export class PostsController {
  constructor(private readonly postsService: PostsService) { }

  /**
   * 创建文章
   * @param post
   */
  @ApiOperation({ summary: '创建文章' })
  @Post('/create')
  async create(@Body() post: CreatePostDto) {
    return await this.postsService.create(post)
  }

  /**
   * 获取所有文章
   * 白名单
   */
  @Public()
  @ApiOperation({ summary: '所有文章' })
  @Get('/findAll')
  async findAll(@Query() query: queryParams): Promise<PostsRo> {
    return await this.postsService.findAll(query)
  }

  /**
   * 获取单个文章
   * @param id
   */
  @Public()
  @Get(':id')
  async findOne(@Param('id') id: number) {
    return await this.postsService.findOne(id)
  }

  /**
   * 更新文章
   * @param id
   * @param post
   */
  @Put(':id')
  async update(@Param('id') id: number, @Body() post: UpdatePostDto) {
    return await this.postsService.updateById(id, post)
  }

  /**
   * 删除
   * @param id
   */
  @Delete('id')
  async remove(@Param('id') id: number) {
    await this.postsService.remove(id)
    return null
  }
}
